﻿using System;
using System.Collections.Generic;
using System.Linq;
using DocumentFormat.OpenXml.Wordprocessing;

namespace ElfDocHelpers
{

    public static partial class ElfDocHelperExtensions
    {

        public static string GetTag(this SdtElement contentControl)
        {

            //-Declaration

            // return value of this function
            string propertyValue  ;

            //-Code

            // the property required ...
            var requiredChildProperty = contentControl.SdtProperties
                                        .GetFirstChild<Tag>();

            // get the value by GetFirstChild generic method call on the properties list ...
            if (requiredChildProperty != null)
            {
                propertyValue = contentControl.SdtProperties
                                    .GetFirstChild<Tag>()
                                    .Val
                                    .Value;
            }
            else {
                propertyValue = null;
            }

            //-Return
            return propertyValue;

        }

        public static int GetControlID( this SdtElement contentControl )
        {

            //-Declaration

            // return value of this function
            int propertyValue  ;

            //-Code

            // the property required ...
            var requiredChildProperty = contentControl.SdtProperties
                                        .GetFirstChild<SdtId>();

            // get the value by GetFirstChild generic method call on the properties list ...
            if (requiredChildProperty != null){
                propertyValue = contentControl.SdtProperties
                                    .GetFirstChild<SdtId>()
                                    .Val
                                    .Value;
            }
            else {
                propertyValue = 0;
            }

            //-Return
            return propertyValue ;

        }

        public static string GetAlias( this SdtElement contentControl )
        {

            //-Declaration

            // return value of this function
            string propertyValue;

            //-Code

            // the property required ...
            var requiredChildProperty = contentControl.SdtProperties
                                        .GetFirstChild<SdtAlias>();

            // get the value by GetFirstChild generic method call on the properties list ...
            if (requiredChildProperty != null)
            {
                propertyValue = contentControl.SdtProperties.GetFirstChild<SdtAlias>().Val.Value;
            }
            else
            {
                propertyValue = null;
            }

            //-Return
            return propertyValue ;

        }

        public static void PokeValueToContentControl(this SdtElement contentControl , string modelPropertyValueString)
        {
            
            //-Declaration

            string ccTypeName = contentControl.GetType().Name;

            //-Code

            switch (ccTypeName)
            {
                case "SdtRun":
                    SdtRun thisRun = (SdtRun)contentControl;
                    var firstDescendant1  = thisRun.Descendants<Text>().FirstOrDefault() ; 
                    if( firstDescendant1 != null )
                    {
                        thisRun.Descendants<Text>().First().Text = modelPropertyValueString;
                    }
                    break;
                case "SdtBlock":
                    SdtBlock thisRun2 = (SdtBlock)contentControl;
                    var firstDescendant2  = thisRun2.Descendants<Text>().FirstOrDefault() ;
                    if (firstDescendant2 != null)
                    {
                        thisRun2.Descendants<Text>().First().Text = modelPropertyValueString;
                    }
                    break;
                case "SdtCell":
                    SdtCell thisRun3 = (SdtCell)contentControl;
                    var firstDescendant3  = thisRun3.Descendants<Text>().FirstOrDefault() ;
                    if (firstDescendant3 != null)
                    {
                        thisRun3.Descendants<Text>().First().Text = modelPropertyValueString;
                    }
                    break;

            }
        }
    }
}
